package com.l.d.app.library.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ArrayRes;
import android.support.annotation.BoolRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntegerRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StringRes;

/**
 * Created by 林德 on 2017/4/5 15:01.
 * <p/>
 */
public final class ResourceUtil
{

    public static Drawable drawable(@NonNull Context context, @DrawableRes int redId)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            return context.getResources().getDrawable(redId, context.getTheme());
        } else
        {
            //noinspection deprecation
            return context.getResources().getDrawable(redId);
        }
    }

    public static int color(@NonNull Context context, @ColorRes int colorResID)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            return color(context, colorResID, context.getTheme());
        }
        //noinspection deprecation
        return context.getResources().getColor(colorResID);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static int color(@NonNull Context context, @ColorRes int color, @Nullable Resources.Theme theme)
    {
        return context.getResources().getColor(color, theme);
    }

    public static String string(@NonNull Context context, @StringRes int string)
    {
        return context.getResources().getString(string);
    }

    public static String[] array(@NonNull Context context, @ArrayRes int array)
    {
        return context.getResources().getStringArray(array);
    }

    /** @return 返回当前设备的绝对尺寸，类似像素，但不是像素值 */
    public static float dimension(@NonNull Context context, @DimenRes int dimen)
    {
        return context.getResources().getDimension(dimen);
    }

    /** @return 返回当前设备的绝对尺寸（像素），并对小数进行四舍五入且非零 */
    public static int dimensionPixelSize(@NonNull Context context, @DimenRes int dimen)
    {
        return context.getResources().getDimensionPixelSize(dimen);
    }

    /** @return 返回当前设备的绝对尺寸（像素），并截断小数，可能为零 */
    public static int getDimensionPixelOffset(@NonNull Context context, @DimenRes int dimen)
    {
        return context.getResources().getDimensionPixelOffset(dimen);
    }

    public static int integer(@NonNull Context context, @IntegerRes int integer)
    {
        return context.getResources().getInteger(integer);
    }

    public static boolean bool(@NonNull Context context, @BoolRes int bool)
    {
        return context.getResources().getBoolean(bool);
    }
}
